package prickworlds.physics;

import java.util.ArrayList;

import org.lwjgl.util.vector.Vector2f;

/**
 * 
 * @author Faruk Acibal, Richard Dziambor, Christian Holzreuter, Nicolas Winkler
 * 
 */
public class RigidBody {
	/**
	 * stores the position of the body
	 */
	Vector2f pos;

	/**
	 * stores the velocity of the body
	 */
	Vector2f vel;

	/**
	 * {@code true}, if this object is not influenced by any kind of collision
	 */
	boolean fix;

	ArrayList<RigidBodyListener> rigidBodyListeners;

	public RigidBody() {
		pos = new Vector2f();
		rigidBodyListeners = new ArrayList<RigidBodyListener>();
	}

	public boolean isFix() {
		return fix;
	}

	public void setFix(boolean fix) {
		this.fix = fix;
	}

	public void setPos(float posX, float posY) {
		pos.x = posX;
		pos.y = posY;
	}

	public void addRigidBodyListener(RigidBodyListener rbl) {
		rigidBodyListeners.add(rbl);
	}

	public void removeRigidBodyListener(RigidBodyListener rbl) {
		rigidBodyListeners.remove(rbl);
	}

	public void processCollision(RigidBody rigidBody) {
		if (rigidBody instanceof RectRigidBody && this instanceof RectRigidBody) {
			if (((RectRigidBody) this).moveAside((RectRigidBody) rigidBody)) {
				causeRigidBodyUpdate();
				rigidBody.causeRigidBodyUpdate();
			}
		}
	}
	
	protected void causeRigidBodyUpdate() {
		for (int i = 0; i < rigidBodyListeners.size(); i++) {
			rigidBodyListeners.get(i).rigidBodyUpdated();
		}
	}

	public float getPosX() {
		return pos.x;
	}

	public float getPosY() {
		return pos.y;
	}

	@Override
	public String toString() {
		return "RigidBody [fix=" + fix + ", pos=" + pos
				+ ", rigidBodyListeners=" + rigidBodyListeners + ", vel=" + vel
				+ "]";
	}

	public boolean overlaps(RigidBody hitbox) {
		return false;
	}
}
